import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yishop/config/EventBus.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:flutter_yishop/utils/toast_util.dart';
import 'package:flutter_yishop/widget/JdButton.dart';
import 'package:flutter_yishop/widget/pay/PayWidget.dart';

class PayPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _PayPageState();
  }
}

class _PayPageState extends State<PayPage> {
  List payList = [
    {
      "title": "支付宝支付",
      "chekced": true,
      "image": "https://www.itying.com/themes/itying/images/alipay.png"
    },
    {
      "title": "微信支付",
      "chekced": false,
      "image": "https://www.itying.com/themes/itying/images/weixinpay.png"
    }
  ];

  bool showKeyword = false;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("支付"),
      ),
      body:
      showKeyword?
      Container(
        child:PayWidget(onConfirmTap: (pwd){
          if (pwd.length != 6) {
            ToastUtil.showToasts("密码不足6位，请重试");
            return;
          }
          print('pwd:$pwd');
          eventBus.fire(new PayEvent("支付"));

        },) ,
      )
      :Column(
        children: <Widget>[
          Container(
              height: 400,
              padding: EdgeInsets.all(20),
              child: ListView.builder(
                itemCount: this.payList.length,
                itemBuilder: (context, index) {
                  return Column(
                    children: <Widget>[
                      ListTile(
                        leading: Image.network(
                          "${this.payList[index]["image"]}",
                          width: ScreenAdapter.width(80),
                          height: ScreenAdapter.height(80),
                        ),
                        title: Text("${this.payList[index]["title"]}"),
                        trailing: this.payList[index]["chekced"]
                            ? Icon(Icons.check)
                            : Text(""),
                        onTap: () {
                          //让payList里面的checked都等于false
                          setState(() {
                            for (var i = 0; i < this.payList.length; i++) {
                              this.payList[i]['chekced'] = false;
                            }
                            this.payList[index]["chekced"] = true;
                          });
                        },
                      ),
                      Divider(),
                    ],
                  );
                },
              )),
          JdButton(
            text: "支付",
            color: Colors.red,
            height: 74,
            cb: () {
             setState(() {
               showKeyword = true;
             });
            },
          )
        ],
      ),
    );
  }

}
